package com.gxa.hystrix.service.impl;

import com.gxa.hystrix.interfaces.ServiceFeignByShow;
import com.gxa.hystrix.service.ShowService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LENOVO
 */
@Service
public class ShowServiceImpl implements ShowService {

    private ServiceFeignByShow serviceFeignByShow;

    @Resource
    public void setServiceFeignByShow(ServiceFeignByShow serviceFeignByShow) {
        this.serviceFeignByShow = serviceFeignByShow;
    }

    /**
     * 通过feign调用服务模块，如果触发熔断机制，则会调用此注解fallbackMethod定义的方法
     *
     * @param name
     * @return
     */
    @Override
    @HystrixCommand(fallbackMethod = "checkError")
    public Map show(String name) {
        return serviceFeignByShow.selectClient(name);
    }

    @Override
    public Map checkError(String name) {
        Map map = new HashMap();
        map.put("code", 500);
        map.put("msg", "服务异常，请稍后重试！");
        map.put("name", name);
        return map;

    }
}
